import 'package:flutter/material.dart';
import 'package:flutter_manager/pages/404.dart';
import 'package:flutter_manager/pages/house/location_list.dart';
import 'package:flutter_manager/pages/notice/index.dart';
import 'package:flutter_manager/pages/tabs_page.dart';
import 'package:flutter_manager/utils/TokenManager.dart';

import 'pages/login/index.dart';
import 'pages/profile/index.dart';
import 'pages/house/house_list.dart';

void main() {
  runApp(MaterialApp(
      routes: {
        "/": (context) => const TabsPage(),
        "/notice_detail": (context) => const NoticeDetail(),
        "/login": (context) => const LoginPage(),
        // "/profile": (context) => const ProfilePage(),
      },
      initialRoute: "/",
      onGenerateRoute: (setting) {
        // 如果跳转了一个没有定义的路由 就会进入onGenerateRoute方法
        // 根据判断 动态生成一个组件
        // profile 的页面有token才可以进入 没有token去登录页
        var token = tokenManager.getToken();
        if (token == "") {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        } else {
          if (setting.name == "/profile") {
            return MaterialPageRoute(
                builder: (context) => ProfilePage(
                      userInfo: setting.arguments as Map<String, dynamic>,
                    ));
          } else if (setting.name == '/house') {
            return MaterialPageRoute(builder: (context) => const HouseList());
          } else if (setting.name == '/add_house') {
            return MaterialPageRoute(
                builder: (context) => const LocationList());
          }
        }
        return MaterialPageRoute(builder: (context) => const NotFound());

        // 这里要立刻返回
      }));
}
